/* Copyright Thanh-Trung Pham, JAIST, 2013 */

package jp.ac.jaist.message;

import java.text.MessageFormat;
import java.util.ResourceBundle;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author Thanh-Trung Pham
 */
public class RMPMessageUtility {
    private static final String JAIST_AC_JP_EXCEPTION_RMMESSAGERESOURCE = "jp.ac.jaist.message.RMPMessageResource";

    private static ResourceBundle m_oResourceBundle;

    /**
     * Get the RMP resource message by ID.
     * @param resouceMessageID the resource message ID.
     * @return the RMP resource message.
     */
    public static String getRMPResourceMessage(String resouceMessageID) {
        if (m_oResourceBundle == null) {
            m_oResourceBundle = ResourceBundle.getBundle(JAIST_AC_JP_EXCEPTION_RMMESSAGERESOURCE);
        }

        return m_oResourceBundle.getString(resouceMessageID);
    }

    /**
     * Get the RMP resource message by ID, replace the pattern {0} with the given parameter.
     * @param resouceMessageID the resource message ID.
     * @param parameter the parameter.
     * @return the RMP resource message.
     */
    public static String getRMPResourceMessage(String resouceMessageID, String parameter) {
        String resourceMessage = getRMPResourceMessage(resouceMessageID);

        return MessageFormat.format(resourceMessage, new Object[]{parameter});
    }

    /**
     * Get the RMP resource message by ID, replace the pattern {0} with the given parameter A, {1} with the given parameter B.
     * @param resouceMessageID the resource message ID.
     * @param parameterA the parameter A.
     * @param parameterB the parameter B.
     * @return the RMP resource message
     */
    public static String getRMPResourceMessage(String resouceMessageID, String parameterA, String parameterB) {
        String resourceMessage = getRMPResourceMessage(resouceMessageID);

        return MessageFormat.format(resourceMessage, new Object[]{parameterA, parameterB});
    }

    /**
     * Get the RMP resource message by ID, replace the pattern {0} with the given parameter A, {1} with the given parameter B, {2} with the given parameter C.
     * @param resouceMessageID the resource message ID.
     * @param parameterA the parameter A.
     * @param parameterB the parameter B.
     * @param parameterC the parameter C.
     * @return the RMP resource message
     */
    public static String getRMPResourceMessage(String resouceMessageID, String parameterA, String parameterB, String parameterC) {
        String resourceMessage = getRMPResourceMessage(resouceMessageID);

        return MessageFormat.format(resourceMessage, new Object[]{parameterA, parameterB, parameterC});
    }

    /**
     * Get the RMP resource message by ID, replace the pattern {0} with the given exception message.
     * @param resouceMessageID the resource message ID.
     * @param exception the exception.
     * @return the RMP resource message.
     */
    public static String getRMPResourceMessage(String resouceMessageID, Exception exception) {
        String resourceMessage = getRMPResourceMessage(resouceMessageID);

        String exceptionMessage = exception.getMessage();
        if(StringUtils.isBlank(exceptionMessage)){
            exceptionMessage = exceptionMessage.getClass().getName();
        }

        return MessageFormat.format(resourceMessage, new Object[]{exceptionMessage});
    }

    /**
     * Get the RMP resource message by ID, replace the pattern {0} with the given parameter, {1} with the given exception message.
     * @param resouceMessageID the resource message ID.
     * @param parameter the parameter.
     * @param exception the exception.
     * @return the RMP resource message.
     */
    public static String getRMPResourceMessage(String resouceMessageID, String parameter, Exception exception) {
        String resourceMessage = getRMPResourceMessage(resouceMessageID);

        String exceptionMessage = exception.getMessage();
        if(StringUtils.isBlank(exceptionMessage)){
            exceptionMessage = exceptionMessage.getClass().getName();
        }

        return MessageFormat.format(resourceMessage, new Object[]{parameter, exceptionMessage});
    }

}
